/**
 * 作者: wgx
 * 日期: 2024-10-17 16:06:31
 * 备注: 用户经营资产表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.FightManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

public class UserAffair extends UserAffairModel {
    public UserAffair() {
        super();
    }

    public JsonObject insert() {
        putResetTime(TimeUtils.getCurTime());
        return super.insert();
    }

    public void reset() {
        if (getEarnTime() == 0) {
            putEarnTime(TimeUtils.getCurTime());
            update();
        }
        if (!TimeUtils.isToday(getResetTime())) {
            putWorship("");
            putBuddyGiftNum(0);
            putBuddyGetGiftNum(0);
            putResetTime(TimeUtils.getCurTime());
            update();
        }
    }

    // 记录日志并增加经验数量
    public void addPartnerExp(UserInfo userInfo, long num, Object... params) {
        long balance = getPartnerExp();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.PARTNER_EXP, dbid, balance, num, params);
        super.addPartnerExp(num);
    }

    // 扣除经验
    public void subPartnerExp(UserInfo userInfo, long num, Object... params) {
        long balance = getPartnerExp();// 当前余额
        if (num > balance) {
            return;// 余额不足
        }
        long dbid = 0;
        MotuLogManager.logItemConsume(userInfo, ItemId.PARTNER_EXP, dbid, balance, num, params);
        super.addPartnerExp(-num);
    }

    // 获取兽潮模块的商业城市收益加成
    public int takeMonsterTideAdd() {
        UserMonsterTideInfo userMonsterTideInfo = FightManager.getUserMonsterTiedInfo(getId());
        if (userMonsterTideInfo == null) {
            return 0;//  未解锁兽潮模块
        }
        if (userMonsterTideInfo.getNextMonsterTideId() == 0) {// 已经全部打完
            return AllParam.MONSTER_TIDE_BUFFER[2];// {500,500,1500};// 兽潮战斗胜利金币收益增加x%, 或是失败减少y%,打赢最后一关的永久收益加成
        }
        if (TimeUtils.getTimeDiff(getMonsterTideBuffStime()) > AllParam.MONSTER_TIDE_BUFFER_DURATION_TIME) {// buffer已经过期
            return 0;
        }
        if (getMonsterTideBuff() == 2) {// 减少的收益
            return -getMonsterTideBuffValue();
        } else {
            return getMonsterTideBuffValue();// 增加的收益
        }
    }
}
